package org.bluesky.controller;

import org.bluesky.framework.ResponseResult;
import org.bluesky.framework.entity.Menu;
import org.bluesky.framework.service.MenuService;
import org.bluesky.framework.service.RoleService;
import org.bluesky.framework.utils.BeanCopyUtils;
import org.bluesky.framework.vo.MenuListVo;
import org.bluesky.framework.vo.MenuRoleVo;
import org.bluesky.framework.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("system/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    public ResponseResult getMenuList(Menu menu){
        return menuService.getMenuList(menu.getMenuName(),menu.getStatus());
    }

    @PostMapping("")
    public ResponseResult addMenu(@RequestBody Menu menu){
        menuService.save(menu);
        return ResponseResult.okResult();
    }

    @GetMapping("/{id}")
    public ResponseResult findMenuById(@PathVariable("id")Long id){
        Menu menu = menuService.getById(id);
        MenuListVo menuVo = BeanCopyUtils.copyBean(menu, MenuListVo.class);
        return ResponseResult.okResult(menuVo);
    }

    @PutMapping("")
    public ResponseResult editMenu(@RequestBody Menu menu){
        if (menu.getId().equals(menu.getParentId())) {
            return ResponseResult.errorResult(500,"修改菜单'" + menu.getMenuName() + "'失败，上级菜单不能选择自己");
        }
        menuService.saveOrUpdate(menu);
        return ResponseResult.okResult();
    }

    @DeleteMapping("/{menuId}")
    public ResponseResult deleteMenu(@PathVariable("menuId")Long menuId){
       return menuService.deleteMenu(menuId);
    }

    @GetMapping("/treeselect")
    public ResponseResult treeSelect(){
        List<MenuRoleVo> menuRoleVos = roleService.treeMenuSelect();
        return ResponseResult.okResult(menuRoleVos);

    }

    @GetMapping("/roleMenuTreeselect/{id}")
    public ResponseResult treeMenuById(@PathVariable("id")Long id){
        return menuService.treeMenuById(id);
    }
}
